package cn.xc.system.controller;

import cn.xc.basic.util.AjaxResult;
import cn.xc.basic.util.PageList;
import cn.xc.system.domain.SystemDictionaryType;
import cn.xc.system.query.SystemDictionaryTypeQuery;
import cn.xc.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典类型api",description = "数据字典类型CRUD")
@RestController
@RequestMapping("/systemDictionaryType")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<SystemDictionaryType> findAll(){
        return systemDictionaryTypeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public SystemDictionaryType findOne(@PathVariable("id") Long id){
        return  systemDictionaryTypeService.findById(id);
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQuery){
        return systemDictionaryTypeService.queryPage(systemDictionaryTypeQuery);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult del(@PathVariable("id") Long id){
        try{
            systemDictionaryTypeService.del(id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try{
            systemDictionaryTypeService.patchDel(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加和修改")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        try{
            if(systemDictionaryType.getId()==null){
                systemDictionaryTypeService.add(systemDictionaryType);
            }else{
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


}
